﻿Imports System.IO
Imports System.Environment

Public Class FlashSavedDataHelper
    Private m_File As String
    Private m_AccsDir As String
    Const FlashDir As String = "\Macromedia\Flash Player\#SharedObjects\"
    Const AccDir As String = "\pio30812.playerio.netdna-cdn.com\EEAccMan\"
    Const EEDir As String = "\pio30812.playerio.netdna-cdn.com\ssx.swf\"
    Const EEFileName As String = "httppio30812playerionetdnacdncom.sol"
    Const PeperFlashDir As String = "\Google\Chrome\User Data\Default\Pepper Data\Shockwave Flash\WritableRoot\#SharedObjects"
    Const PeperAccDir As String = "\p#\io30812.playerio.netdna-cdn.com\EEAccMan\"
    Const PeperEEDir As String = "\p#\io30812.playerio.netdna-cdn.com\ssx.swf\httpp#\"
    Const PeperEEFileName As String = "io30812playerionetdnacdncom.sol"
    Const CurrSettingsVer As UInteger = 1

    Sub New()
        Try
            If My.Settings.SettingsVer < CurrSettingsVer Then
                My.Settings.ChromeMode = IIf(MsgBox("Do you use google chrome?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes, True, False)
                My.Settings.SettingsVer = CurrSettingsVer
                My.Settings.Save()
            End If

            If My.Settings.ChromeMode Then
                'Chrome
                InitPeperFlashDirs()
            Else
                'IE, Firefox, Opera, Safari
                InitFlashDirs()
            End If

            If Not Directory.Exists(m_AccsDir) Then
                Directory.CreateDirectory(m_AccsDir)
            End If
            If Not File.Exists(m_File) Then
                Throw New ApplicationException("Could not find current save data")
            End If
        Catch ex As Exception
            MsgBox("Error finding flash settings! Try logging into EE and try again. Error: " & ex.ToString & ", " & ex.Message)
            End
        End Try
    End Sub

    Sub InitFlashDirs()
        Dim myDirs As String() = Directory.GetDirectories(Environment.GetFolderPath(SpecialFolder.ApplicationData) & FlashDir)
        Dim myDir As String = myDirs(0) & EEDir
        m_File = myDirs(0) & EEDir & EEFileName
        m_AccsDir = myDirs(0) & AccDir
    End Sub

    Sub InitPeperFlashDirs()
        Dim myDirs As String() = Directory.GetDirectories(Environment.GetFolderPath(SpecialFolder.LocalApplicationData) & PeperFlashDir)
        Dim myDir As String = myDirs(0) & PeperEEDir
        m_File = myDir & PeperEEFileName
        m_AccsDir = myDirs(0) & AccDir
    End Sub

    Function LoadSavedAccs() As String()
        Dim myAccs As String() = Directory.GetFiles(m_AccsDir)
        For N = 0 To myAccs.Length - 1
            myAccs(N) = Path.GetFileNameWithoutExtension(myAccs(N))
        Next
        Return myAccs
    End Function

    Sub AddCurrentAcc(Name As String)
        If Not AccExists(Name) Then
            File.WriteAllBytes(m_AccsDir & Name & ".sol", File.ReadAllBytes(m_File))
        End If
    End Sub

    Function AccExists(Name As String) As Boolean
        Return File.Exists(m_AccsDir & Name & ".sol")
    End Function

    Sub SwitchCurrentAcc(Name As String)
        If AccExists(Name) Then
            File.WriteAllBytes(m_File, File.ReadAllBytes(m_AccsDir & Name & ".sol"))
        End If
    End Sub

    Sub RemoveAcc(Name As String)
        If AccExists(Name) Then
            File.Delete(m_AccsDir & Name & ".sol")
        End If
    End Sub
End Class
